package com.csdj.server.controller;

import com.csdj.server.pojo.Admin;
import com.csdj.server.pojo.FastDFSFile;
import com.csdj.server.pojo.RespBean;
import com.csdj.server.service.IAdminService;
import com.csdj.server.util.FastDFSClient;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @Author zyq
 * @Date 2023/2/13 10:39
 * @ClassName: AdminInfoController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
public class AdminInfoController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "更新当前用户信息")
    @PutMapping("/admin/info")
    public RespBean updateAdmin(@RequestBody Admin admin, Authentication authentication){
        //修改 用户信息
        if(adminService.updateById(admin)){
            //重构 security全局对象中 存储的用户信息
            //1.用户对象
            //2. authentication.getCredentials() 用户凭证 密码
            //3. 权限信息
            SecurityContextHolder.getContext().setAuthentication(
                    new UsernamePasswordAuthenticationToken(admin,
                            authentication.getCredentials(),
                            authentication.getAuthorities()));
            return RespBean.success("更新成功!");
        }
        return RespBean.error("修改失败!");
    }

    @ApiOperation(value = "修改密码")
    @PutMapping("/admin/pass")
    public RespBean updatePass(@RequestBody Map<String,Object> map){

        String oldPass = (String) map.get("oldPass");
        String password = (String) map.get("password");
        Integer adminId = (Integer) map.get("adminId");
      return  adminService.updatePassword(oldPass,password,adminId);
    }

    @ApiOperation(value = "上传头像")
    @PostMapping("/admin/userface")
    public  RespBean updateHrUserFace(MultipartFile file,Integer id,Authentication authentication){

        String url = saveFile(file);

        return adminService.updateAdminFace(url,id,authentication);

    }


    public String saveFile(MultipartFile multipartFile){

        //获取到文件名
        String filename = multipartFile.getOriginalFilename();
        //根据文件名 获取文件扩展名
        String ext = filename.substring(filename.lastIndexOf(".") +1);
        InputStream inputStream=null;
        byte[] buff_byte=null;
        String[] upload=null;
        try {
            //传输的文件 放入 字节输入流
            inputStream = multipartFile.getInputStream();
            if(inputStream!=null){
                //获取到 文件的总 字节数
                int available = inputStream.available();
                //根据文件大小 创建 对应的字节数组
                buff_byte=new byte[available];
                //读到 字节数组
                inputStream.read(buff_byte);
            }
            inputStream.close();
            FastDFSFile fastDFSFile = new FastDFSFile(filename,buff_byte,ext);
            upload = FastDFSClient.upload(fastDFSFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //http://ip:8888/group1/M00/00/00
        return  FastDFSClient.getTrackServerPath()+upload[0]+"/"+upload[1];
    }


}
